package top.wintp.offlinedataanalysis.anlyser.dim.base;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang.StringUtils;

import jdk.nashorn.internal.runtime.GlobalConstants;
import top.wintp.offlinedataanalysis.common.ConstantValues;

/**
 * 平台维度类
 *
 * @author root
 */
public class PlatformDimension extends BaseDimension {

    private int id;
    /**
     * 平台名称
     */
    private String platformName;

    @Override
    public String toString() {
        return "PlatformDimension{" +
                "id=" + id +
                ", platformName='" + platformName + '\'' +
                '}';
    }

    public PlatformDimension() {
        super();
    }

    public PlatformDimension(String platformName) {
        super();
        this.platformName = platformName;
    }

    public PlatformDimension(int id, String platformName) {
        super();
        this.id = id;
        this.platformName = platformName;
    }

    /**
     * 构建平台模块集合 默认添加了所有平台的情况
     *
     * @param platformName
     * @return
     */
    public static List<PlatformDimension> buildList(String platformName) {
        if (StringUtils.isBlank(platformName)) {
            platformName = ConstantValues.DEFAULT_VALUE;
        }
        List<PlatformDimension> list = new ArrayList<PlatformDimension>();
        list.add(new PlatformDimension(ConstantValues.VALUE_OF_ALL));
        list.add(new PlatformDimension(platformName));
        return list;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPlatformName() {
        return platformName;
    }

    public void setPlatformName(String platformName) {
        this.platformName = platformName;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(this.id);
        out.writeUTF(this.platformName);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.id = in.readInt();
        this.platformName = in.readUTF();
    }

    @Override
    public int compareTo(BaseDimension o) {
        if (this == o) {
            return 0;
        }

        PlatformDimension other = (PlatformDimension) o;
        int tmp = Integer.compare(this.id, other.id);
        if (tmp != 0) {
            return tmp;
        }
        tmp = this.platformName.compareTo(other.platformName);
        return tmp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        PlatformDimension that = (PlatformDimension) o;
        return id == that.id &&
                Objects.equals(platformName, that.platformName);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, platformName);
    }
}
